package com.liruan.rest.dto;

import java.util.List;
import com.liruan.rest.po.UserInfoPO;
import com.liruan.rest.service.result.ResultCode;

public final class DTOFactory
{
	public static AuthTokenDTO createAuthTokenDTO(String token, Long userId, ResultCode resultCode)
	{
		AuthTokenDTO result = new AuthTokenDTO(resultCode);
		if (token == null)
		{
			return result;
		}

		result.token = token;
		result.userId = userId;
		return result;
	}

	public static UserInfoDTO createUserInfoPODTO(UserInfoPO userInfo, ResultCode resultCode)
	{
		UserInfoDTO dto = new UserInfoDTO(resultCode);
		if (userInfo == null)
		{
			return dto;
		}
		dto.setId(userInfo.getId());
		return dto;
	}

	public static ResultCodeOnlyDTO createResultCodeOnlyDTO(ResultCode resultCode)
	{
		return new ResultCodeOnlyDTO(resultCode);
	}

	public static <T extends DTO> ListDTO<T> createListDTO(List<T> data,
	        final ResultCode resultCode)
	{
		ListDTO<T> res = new ListDTO<T>(resultCode);
		if (data == null)
		{
			return res;
		}

		res.list = data;
		return res;
	}

	public static <T extends DTO> PageDTO<T> createPageDTO(List<T> data,
	        Integer total, final ResultCode resultCode)
	{
		PageDTO<T> res = new PageDTO<T>(resultCode);
		if (data == null)
		{
			return res;
		}

		res.list = data;
		res.size = data.size();
		res.total = total;

		return res;
	}

}
